﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Xml;
using EntityAop.Configuration;

namespace EntityAop.Extensions
{
    public static class ObjectExtension
    {
        /// <summary>
        /// Creates proxied instance regarding to RrslLite configuration
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance"></param>
        /// <returns></returns>
        public static T CreateProxy<T>(this T instance)
        {
            return EntityAopConfiguration.Instance.CreateProxiedInstance(instance);
        }

        public static IList<T> Unproxy<T>(this ObservableCollection<T> instance)
        {
            return (IList<T>)EntityAopConfiguration.Instance.UnproxyInstance(instance);
        }

        public static T Unproxy<T>(this T instance)
        {
            // to Configuration add unproxy method

            return (T)EntityAopConfiguration.Instance.UnproxyInstance(instance);
        }

#if !SILVERLIGHT
        public static T DeserializeEmbeddedResource<T>(this T instance, string embeddedResourceName)
        {
            var callingAssembly = Assembly.GetCallingAssembly();

            var assemblyName = callingAssembly.GetName().Name;

            var resourceStream =
                callingAssembly.GetManifestResourceStream(string.Format("{0}.{1}", assemblyName, embeddedResourceName));

            return XmlSerializerHelper.DeserializeObject<T>(new XmlTextReader(resourceStream));
        }
#endif
    }
}
